using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pheonix.Fashionzoomer.Entity.SqlServer2000;
using Pheonix.CodeLibrary;
using Pheonix.Fashionzoomer.DAL;

/// <summary>
/// Summary description for NewsletterManager
/// </summary>

namespace Pheonix.Fashionzoomer.BLL
{
    public sealed class NewsletterManager
    {
        private NewsletterManager(){}

        public static ActionResult AddEmailToNewsletterList(string email)
        {
            if (!String.IsNullOrEmpty(email) && RegExUtil.IsEmail(email))
            {
                if (!CustomCommand.IsAlreadySubscribedForNewsletter(email))
                {
                    Subscribe subscribe = new Subscribe();
                    subscribe.Email = email;
                    try
                    {
                        if (subscribe.Save())
                            return new ActionResult(ActionResultType.Succeed, "Successfuly subscribe!");
                        else
                            return new ActionResult(ActionResultType.Failed, "Subscription failed");
                    }
                    catch (Exception ex)
                    {
                        return new ActionResult(ActionResultType.Error, ex.Message);
                    }
                }
                else
                    return new ActionResult(ActionResultType.InvalidOperation, "Already subscribed");
            }
            else
                return new ActionResult(ActionResultType.InvalidOperation,"Invalid email");
        }

        public static ActionResult RemoveEmailFromNewsletterList(string email)
        {
            if (!String.IsNullOrEmpty(email) && RegExUtil.IsEmail(email))
            {
                try
                {
                    if (CustomCommand.DeleteSubscribeByEmail(email))
                        return new ActionResult(ActionResultType.Succeed, "Successfuly subscribe!");
                    else
                        return new ActionResult(ActionResultType.Failed, "Subscription failed");
                }
                catch (Exception ex)
                {
                    return new ActionResult(ActionResultType.Error, ex.Message);
                }
            }
            else
                return new ActionResult(ActionResultType.InvalidOperation, "Invalid email");
        }
    }
}